package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.SettleCalculatePriceEntity;
import cn.mumzone.pos.util.PopupUtil;

/**
 * Created by qiuzhenhuan on 2019/6/17.
 */

public class ADA_DiscountList extends CommonAdapter<SettleCalculatePriceEntity.DiscountItemBean> {

    private PopupWindow mPopHint;

    public ADA_DiscountList(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_discount_list;
    }

    @Override
    protected void convert(cn.mumzone.basic.adapter.rv.ViewHolder holder, final SettleCalculatePriceEntity.DiscountItemBean discountItemBean, int position) {
        holder.setText(R.id.tv_discount_name, discountItemBean.getName());
        holder.setText(R.id.tv_discount_amount, StringUtils.getFormattedMoneyWithUnit(discountItemBean.getDiscount()));

        holder.setOnClickListener(R.id.ic_cancel_discount, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupUtil.dismissPopup(PopupUtil.SETTLE_RECALL_DISCOUNT, mPopHint);
                if(onDeleteDiscountListener!=null){
                    onDeleteDiscountListener.onDeleteDiscount(discountItemBean);
                }
            }
        });

        if (position == 0) {
            showPopupHint(holder.getView(R.id.ic_cancel_discount));
        }
    }

    private OnDeleteDiscountListener onDeleteDiscountListener;

    public void setOnDeleteDiscountListener(OnDeleteDiscountListener onDeleteDiscountListener) {
        this.onDeleteDiscountListener = onDeleteDiscountListener;
    }

    public interface OnDeleteDiscountListener{
        void onDeleteDiscount(SettleCalculatePriceEntity.DiscountItemBean discountItemBean);
    }

    public void showPopupHint(final View targetView) {
        if (mPopHint != null) return;
        targetView.postDelayed(new Runnable() {
            @Override
            public void run() {
                mPopHint = PopupUtil.showBlackBgPopup(targetView,
                        PopupUtil.SETTLE_RECALL_DISCOUNT,
                        "点击撤销结算方式", PopupUtil.RIGHT_TOP );

            }
        }, 500);
    }

}
